<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Fb extends MY_Controller 
{
    public function __construct()
    {	
        parent::__construct();
        
        $this->load->model('fb/fb_page_posts_model');
    }
    
    public function test()
    {  
        require 'application/libraries/php-sdk/facebook.php';
    
        // Create our Application instance (replace this with your appId and secret).
        $facebook = new Facebook(array(
          	'appId'  => '271720459674895',
          	'secret' => 'd963b9ef3de25a725eb89fb562dbea36',
        ));
        
        // Get User ID
        $userId = $facebook->getUser();
  
        if ($userId) {
            try {
                /* make the API call */
                // shows only the posts that were published by this page.
                // https://developers.facebook.com/docs/reference/api/post/
                //$postsArr = $facebook->api('/TranMyStore/posts?fields=from,link', 'GET');
                $postsArr = $facebook->api('/testappvn/posts', 'GET');
                
//                 $postsData = array();
//                 foreach ($postsArr['data'] as $postArr) {
//                     $_postsData = array('name' => $postArr['from']['name'],
//                     				    'link' => $postArr['link'], 
//                                         'picture' => str_replace('_s.', '_n.', $postArr['picture']));
//                     array_push($postsData, $_postsData);
//                 }
                
//                 $this->fb_page_posts_model->clonePosts($postsData);
                
                header('Content-Type: application/json');
                echo json_encode($postsArr);
                return $result;
                
            } catch(FacebookApiException $e) {
                Epic_Debug::dump($e);
            }   
        }
        
        // Login or logout url will be needed depending on current user state.
        $logoutUrl = '';
        $loginUrl = '';
        $statusUrl = '';
        if ($userId) {
            $logoutUrl = $facebook->getLogoutUrl();
        } else {
            // Check the login status using OAuth 2.0 handled by the PHP SDK:
            $statusUrl = $facebook->getLoginStatusUrl();
            // Login using OAuth 2.0 handled by the PHP SDK:
            $loginUrl =  $facebook->getLoginUrl(array(
              'scope' => 'read_stream',
            ));

        }
        
        $this->render(array(
        	'userId' => $userId,
        	'logoutUrl' => $logoutUrl,
        	'loginUrl' => $loginUrl,
            'statusUrl' => $statusUrl
        ));
    }
    
    public function index()
    {
        require 'application/libraries/php-sdk/facebook.php';
        
        $config = array(
            'appId' => '271720459674895',
            'secret' => 'd963b9ef3de25a725eb89fb562dbea36',
            'fileUpload' => false, // optional, whether or not file uploads are enabled on your server
            // For security purposes, this should be set to false for non-canvas apps
            'allowSignedRequest' => false, // optional, whether or not to use signed_request data from query parameters or the POST body
        );
        
        // Create our Application instance
        $facebook = new Facebook($config);
        
        // Get User ID
        $user = $facebook->getUser();
        var_dump($user);die;
        
        $this->render();
    }
    
    public function login_with_js()
    {
        $this->render();
    }
    
    // covers posting a link to the timeline using the Graph API with the PHP SDK
    public function share_link()
    {
        $this->render();
    }
}